<template>
  <div id="cart-page">

    <div style="padding-top: 100px">
      <el-input
        placeholder="请输入验证码"
        v-model.trim="form.verify"
        clearable>
      </el-input>
      <img class="verfiyImg" ref="verfiyImg" title="点击刷新"
           :src="verifyImgSrc"
           @click="reflshVerifyImg"
           alt="">
      点击 <el-button type="primary" @click="login">重新登陆</el-button>


    </div>
  </div>
</template>

<script>
import CartTools from '../tools/CartTools'

export default {
  name: 'Cart',
  data: function () {
    return {
      verifyImgSrc: this.ApiDoNameAuth + 'Verify/createImage?eventType=login&deviceId=' + this.GetDeviceID(),
      isShifu:this.CurrUser.isShifu,
      allList: CartTools.getAllList(),
      productSelectMap:{},
      productNewModelMap:{},
      selectedNums:0,
      shopSelectArr:[],
      form:{
        number: '307000004',
        username: 'admin',
        password: 'a123456',
        verify: ''
      }
    }
  },
  mounted () {
    // localStorage.setItem("CartCache",'[{"shop":{"id":1,"logo":"http://image.yangfankj.com/shop/logo/1.jpg","name":"华盛五金店"},"productList":[{"id":2,"image":"http://image.yangfankj.com/product/104/0.jpg","name":"德力西-冲击钻","price":265,"model":"常规","nums":1},{"id":1,"image":"http://image.yangfankj.com/product/110/0.jpg","name":"威马-电动螺丝刀","price":122.05,"model":"大,黑","nums":1},{"id":3,"image":"http://image.yangfankj.com/product/103/0.jpg","name":"德国欧莱德-磨角机","price":215,"model":"常规","nums":1}]},{"shop":{"id":2,"logo":"http://image.yangfankj.com/shop/logo/2.jpg","name":"飞扬五金"},"productList":[{"id":5,"image":"http://image.yangfankj.com/product/105/0.jpg","name":"东成-气动直钉枪","price":195,"model":"常规","nums":1},{"id":6,"image":"http://image.yangfankj.com/product/104/0.jpg","name":"德力西-冲击钻","price":275,"model":"常规","nums":1}]},{"shop":{"id":3,"logo":"http://image.yangfankj.com/shop/logo/3.jpg","name":"国红五金水暖店"},"productList":[{"id":11,"image":"http://image.yangfankj.com/product/105/0.jpg","name":"东成-气动直钉枪","price":195,"model":"常规","nums":1}]},{"shop":{"id":4,"logo":"http://image.yangfankj.com/shop/logo/4.jpg","name":"阳蕙五金店"},"productList":[{"id":12,"image":"http://image.yangfankj.com/product/110/0.jpg","name":"威马-电动螺丝刀","price":135,"model":"常规","nums":1}]},{"shop":{"id":7,"logo":"http://image.yangfankj.com/shop/logo/7.jpg","name":"唐久便利"},"productList":[{"id":20,"image":"http://image.yangfankj.com/product/7/0/0.png","name":"百事可乐","price":5,"model":"常规","nums":1}]},{"shop":{"id":6,"logo":"http://image.yangfankj.com/shop/logo/6.jpg","name":"金三角五金水暖经销部"},"productList":[{"id":19,"image":"http://image.yangfankj.com/product/105/0.jpg","name":"东成-气动直钉枪","price":195,"model":"常规","nums":1}]},{"shop":{"id":8,"logo":"http://image.yangfankj.com/shop/logo/8.jpg","name":"人人乐"},"productList":[{"id":25,"image":"http://image.yangfankj.com/product/7/0/0.png","name":"百事可乐","price":5,"model":"常规","nums":1},{"id":26,"image":"http://image.yangfankj.com/product/8/9/0.png","name":"山西汾酒","price":86,"model":"常规","nums":1},{"id":27,"image":"http://image.yangfankj.com/product/8/5/0.png","name":"欧莱雅 紫熨斗眼霜","price":145,"model":"常规","nums":1}]}]')
  },
  methods: {
    reflshVerifyImg: function () {
      this.$refs.verfiyImg.src = this.verifyImgSrc + '&v=' + Math.random()
      this.form.verify = ''
    },
    login:function () {
      let $this=this
      $this.MyRequest($this.ApiDoNameAuth + 'Public/login', {
        number: $this.form.number,
        username: $this.form.username,
        password: $this.form.password,
        imageVerifyCode: $this.form.verify,
      }, function (data) {
        $this.isLoading = false
        $this.CurrUser.setCache({
          token: data.token,
          username: $this.form.username,
          number: $this.form.number,
        })
        $this.$router.push('/OrderList')
        // location.href='/'
      }, function (data) {
        $this.isLoading = false
        $this.successTip(data.message, 'error')
        // $this.form.password = ''
        $this.reflshVerifyImg()
      })
    },
    getNewPriceModel:function (productItem,key) {
      var map=this.productNewModelMap[productItem.id]
      if (map){
        return map[key]
      }else{
        return productItem[key]
      }
    },
    chooseModel:function (productParams){
      if (productParams.model.indexOf(',')===-1){
        return
      }
      productParams.currModel=this.getNewPriceModel(productParams,'model')
      let $this=this
      $this.bus.$emit('ProductChooseModelDialogCallBus','cartPage',productParams,function (modelData) {
        $this.allList.forEach(function (item,index) {
          $this.allList[index].shop.select=false
          item.productList.forEach(function (_value, _index) {
            if (_value.id===productParams.id){
              _value.model=modelData.model
              _value.price=modelData.price
              $this.$set($this.productNewModelMap,productParams.id,{
                model:modelData.model,
                price:modelData.price,
              })
            }
          })
        })

        // 同步缓存
        CartTools.setAllList($this.allList)
        $this.$forceUpdate()
      })
    },
    // 准备结算
    gotoBuy: function () {
      if (this.getIsShifu()){
        // 有师傅
        let $this=this
        var selectData = this.getSelectData()
        $this.MyRequest($this.ApiDoNameShop + 'Member/isBindingShop', {shopId:selectData.shop.id}, function (isBinding) {
          $this.bus.$emit('CartSubmitDialogCallBus',isBinding,function (submitType) {
            $this.getSelectData()
            if (submitType===0){
              $this.submitOrder()
            }
            if (submitType===1){
              $this.submitShopProductPurchaseOrder()
            }
          })
        }, function (msg) {
          $this.$message.error(msg)
        })
      }else{
        // 没有师傅绑定
        this.submitOrder()
      }
    },
    getSelectData:function () {
      let $this=this
      var selectData={}
      $this.allList.forEach(function (item,index){
        if (item.shop.select){
          var productList=[]
          item.productList.forEach(function (value, index) {
            if ($this.productSelectMap[value.id]===true){
              productList.push(value)
            }
          })
          if (productList.length>0){
            selectData=Object.assign({},item)
            selectData.productList=productList
          }
        }
      })
      localStorage.setItem('selectProductData',JSON.stringify(selectData))
      return selectData
    },
    // 提交-采购单
    submitShopProductPurchaseOrder:function () {
      this.getSelectData()
      this.$router.push("/ConfirmPurchaseOrder")
    },
    // 提交-订单
    submitOrder:function  () {
      this.getSelectData()
      this.$router.push("/ConfirmOrder")
    },
    changeProductNums:function (productParams) {
      CartTools.setAllList(this.allList)
    },
    // 更改商品-数量
    changeSelectNums: function () {
      let $this=this
      var keysArr = Object.keys($this.productSelectMap)
      var i=0
      keysArr.forEach(function (key,index) {
        if ($this.productSelectMap[key]){
          i++
        }
      })
      $this.shopSelectArr=[]
      var shopIndex=-1
      $this.allList.forEach(function (item,index) {
        item.productList.forEach(function (_value, _index) {
          if ($this.productSelectMap[_value.id]===true){
            console.log('item',item)
            shopIndex=index
            if ($this.shopSelectArr.indexOf(item.shop.id)===-1){
              $this.shopSelectArr.push(item.shop.id)
            }
          }
        })
      })
      $this.selectedNums = i
      // console.log('$this.allList',$this.allList)
      if (shopIndex>-1){
        $this.$set($this.allList[shopIndex].shop,'select',true)
      }
      $this.$forceUpdate()
    },
    // 删除-单个店铺-所有商品
    delShop:function (index) {
      this.$delete(this.allList,index)
    },
    // 删除-单个商品
    delItem: function (product) {
      CartTools.delProduct(product.id,product.model)
      this.allList=CartTools.getAllList()
      this.$forceUpdate()
    },
    handleList:function () {
      let $this=this
      $this.allList.forEach(function (item,index) {
        $this.allList[index].shop.select=false
        item.productList.forEach(function (_value, _index) {
          $this.allList[index].productList[_index].select=false
          $this.$set($this.productSelectMap,_value.id,false)
        })
      })
      $this.selectedNums=0
      $this.$forceUpdate()
    },
    checkShopSelect:function (e,shopItem) {
      let $this=this
      $this.allList.forEach(function (item,index) {
        if (item.shop.id===shopItem.id){
          item.productList.forEach(function (_value, _index) {
            $this.allList[index].productList[_index].select=shopItem.select
            $this.$set($this.productSelectMap,_value.id,shopItem.select)
          })
        }
      })
      $this.changeSelectNums(shopItem.id)
      $this.$forceUpdate()
    },
  }
}
</script>

<style scoped>

</style>
